﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FaceDectionSiliverlight
{

    public struct FaceFeaturePoint
    {
        public int x;
        public int y;
    }
    public struct QSFacePointsModel
    {
        public float pitch;
        public FaceFeaturePoint[] points;
        public float roll;
        public float yaw;
    }
    public enum AntiFaceAction
    {
        UnKnown=0,
        /// <summary>
        /// 点头
        /// </summary>
        UpOrDown=1,

        /// <summary>
        /// 摇头
        /// </summary>
        LeffOrRight,

        /// <summary>
        /// 睁眼
        /// </summary>
        OPEN_EYE,

        /// <summary>
        /// 闭眼
        /// </summary>
        CLOSE_EYE,

        /// <summary>
        /// 张嘴
        /// </summary>
        OPEN_MOUNTH,

        /// <summary>
        /// 闭嘴
        /// </summary>
        CLOSE_MOUTH
    }

    public static class AntiFaceHelper
    {

         const  float UPORDOWN=10.0f;

         const  float LEFTORRIGHT=10.0f;

         const  float OPEN_EYE=6.0f;

         const float CLOSE_EYE = 5.0f;

         const float OPEN_MOUNTH = 10.0f;

         const float CLOSE_MOUTH = 5.0f;


         public static bool Verity(float pitch, float roll, float yaw, FaceFeaturePoint[] point, AntiFaceAction action)
        {
            if (point != null)
            {
                switch (action)
                {
                    case AntiFaceAction.CLOSE_EYE:
                        if (point.Length > 24)
                        {
                            int x = Math.Abs(Convert.ToInt32(point[24].y - point[20].y));
                            if (x < CLOSE_EYE)
                                return true;
                        }
                        break;
                    case AntiFaceAction.OPEN_EYE:
                        if (point.Length > 24)
                        {
                            int x = Math.Abs(Convert.ToInt32(point[24].y - point[20].y));
                            if (x > OPEN_EYE)
                                return true;
                        }
                        break;
                    case AntiFaceAction.CLOSE_MOUTH:
                        if (point.Length > 47)
                        {
                            int x = Math.Abs(Convert.ToInt32(point[47].y - point[44].y));
                            if (x < CLOSE_MOUTH)
                                return true;
                        }
                        break;
                    case AntiFaceAction.OPEN_MOUNTH:
                        if (point.Length > 47)
                        {
                            int x = Math.Abs(Convert.ToInt32(point[47].y - point[44].y));
                            if (x > OPEN_MOUNTH)
                                return true;
                        }
                        break;
                    case AntiFaceAction.LeffOrRight:
                        if (Math.Abs(yaw) > LEFTORRIGHT)
                            return true;
                        break;
                    case AntiFaceAction.UpOrDown:
                        if (Math.Abs(pitch) > UPORDOWN)
                            return true;
                        break;
                    default:
                        return false;
                }
            }
            
            return false;
        }
    }
}
